package com.genius.mobile_page.pingjia;

import java.sql.SQLException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.dingding.util.GeniusConfig;
import com.genius.mobile_page.common.ErrorResponseData;
import com.genius.mobile_page.common.ResponseData;
import com.genius.mobile_page.common.SuccessResponseData;
import com.genius.mobile_page.pandian.pandianUtil;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;

import cn.hutool.core.util.ObjectUtil;

public class PingjiaUtil {
	//插入各类型评价的信息
	public static ResponseData insertTypePingjia(JSONObject parms) {
		try {
//			System.out.println(parms);
			DBTools.replace("pingjiaType,id,shopcode,pjId,pingjiaUser,pingjiaUserName,pingjiaFlag=0,lrsj=now()"
					, "pingjia_info_other", parms);
			return new SuccessResponseData();
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("待评价信息新增失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}
	//更改各个类型评价状态
	public static ResponseData updateTypePingjia(JSONObject parms) {
		try {
			DBTools2.update("pingjiaId,pingjiaFlag=1,pingjiaTime=now()", "pingjiaType,id", "pingjia_info_other", parms);
			//盘点系统更改原系统状态
			if(ObjectUtil.equals("1", parms.getString("pingjiaType"))){
				//此处flag应为8，8为已评价状态，id为盘点id,pingjiaFlag为1
				DBTools2.update("flag=8,pingjiaFlag=1", "id", "pandian_jiancha", parms);
			}else if(ObjectUtil.equals("4", parms.getString("pingjiaType"))){
				//此处flag应为8，8为已评价状态，id为盘点id,pingjiaFlag为1
				DBTools2.update("flag=8,pingjiaFlag=1", "id", "pandian_jiancha_jd", parms);
			}else if(ObjectUtil.equals("6", parms.getString("pingjiaType"))){
				//此处flag应为8，8为已评价状态，id为盘点id,pingjiaFlag为1
				DBTools2.update("flag=8,pingjiaFlag=1", "id", "huodong_jiancha_jd_every", parms);
			}
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("评价状态更改失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	//查询各个类型评价
	public static ResponseData getTypePingjia(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select a.*,(select name from dict_info_new where"
				+ " type = 'pingjiaType' and id = a.pingjiaType)pingjiaTypename"
				+ " from pingjia_info_other a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("pingjiaFlag"))){
				sql.append(" and pingjiaFlag = ?");
				params.add(parms.getString("pingjiaFlag"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("pingjiaType"))){
				sql.append(" and pingjiaType = ?");
				params.add(parms.getString("pingjiaType"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
				sql.append(" and shopcode in ("+parms.getString("shopcode")+")");
			}
			if(ObjectUtil.isNotEmpty(parms.getString("pjId"))){
				sql.append(" and pjId = ?");
				params.add(parms.getString("pjId"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("pingjiaUser"))){
				sql.append(" and pingjiaUser = ?");
				params.add(parms.getString("pingjiaUser"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("id"))){
				sql.append(" and id = ?");
				params.add(parms.getString("id"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("endDate"))){
				sql.append(" and lrsj <= ?");
				params.add(parms.getString("endDate")+" 23:59:59");
			}
			if(ObjectUtil.isNotEmpty(parms.getString("startDate"))){
				sql.append(" and lrsj >= ?");
				params.add(parms.getString("startDate")+" 00:00:00");
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			System.out.println(">>>"+sql);
			if(result != null && result.size() > 0){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有查询到数据");
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	
	}
}
